home *** CD-ROM | disk | FTP | other *** search
/ Aminet 32 / Aminet 32 (1999)(Schatztruhe)[!][Aug 1999].iso / Aminet / dev / lang / Python151_Src.lha / Python1.5_Source / Objects / abstract.c next >
C/C++ Source or Header  |  1998-05-30  |  23KB  |  1,126 lines

  1. /***********************************************************
  2. Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
  3. The Netherlands.
  4.  
  5.                         All Rights Reserved
  6.  
  7. Permission to use, copy, modify, and distribute this software and its
  8. documentation for any purpose and without fee is hereby granted,
  9. provided that the above copyright notice appear in all copies and that
  10. both that copyright notice and this permission notice appear in
  11. supporting documentation, and that the names of Stichting Mathematisch
  12. Centrum or CWI or Corporation for National Research Initiatives or
  13. CNRI not be used in advertising or publicity pertaining to
  14. distribution of the software without specific, written prior
  15. permission.
  16.  
  17. While CWI is the initial source for this software, a modified version
  18. is made available by the Corporation for National Research Initiatives
  19. (CNRI) at the Internet address ftp://ftp.python.org.
  20.  
  21. STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
  22. REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
  23. MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
  24. CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
  25. DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  26. PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  27. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  28. PERFORMANCE OF THIS SOFTWARE.
  29.  
  30. ******************************************************************/
  31.  
  32. /* Abstract Object Interface (many thanks to Jim Fulton) */
  33.  
  34. #include "Python.h"
  35. #include "protos/abstract_protos.h"
  36.  
  37. #define Py_TRY(E) if(!(E)) return NULL
  38. #define Py_ASSERT(EXP,E,V) if(!(EXP)) return PyErr_SetString(E,V), (void*)NULL
  39. #define SPAM printf("line %d\n",__LINE__)
  40.  
  41. static PyObject *
  42. Py_ReturnMethodError(name)
  43.   char *name;
  44. {
  45.   if(! name) name = "Unknown Error";
  46.   PyErr_SetString(PyExc_AttributeError,name);
  47.   return 0;
  48. }
  49.  
  50. static PyObject *
  51. Py_ReturnNullError()
  52. {
  53.   if(! PyErr_Occurred())
  54.     PyErr_SetString(PyExc_SystemError,
  55.             "null argument to internal routine");
  56.   return 0;
  57. }
  58.  
  59. int 
  60. PyObject_Cmp(o1, o2, result)
  61.   PyObject *o1;
  62.   PyObject *o2;
  63.   int *result;
  64. {
  65.   int r;
  66.  
  67.   if(! o1 || ! o2) return Py_ReturnNullError(),-1;
  68.   r=PyObject_Compare(o1,o2);
  69.   if(PyErr_Occurred()) return -1;
  70.   *result=r;
  71.   return 0;
  72. }
  73.  
  74. #if 0 /* Already in object.c */
  75. int
  76. PyCallable_Check(x)
  77.   PyObject *x;
  78. {
  79.     if (x == NULL)
  80.         return 0;
  81.     if (x->ob_type->tp_call != NULL ||
  82.         PyFunction_Check(x) ||
  83.         PyMethod_Check(x) ||
  84.         PyCFunction_Check(x) ||
  85.         PyClass_Check(x))
  86.         return 1;
  87.     if (PyInstance_Check(x)) {
  88.         PyObject *call = PyObject_GetAttrString(x, "__call__");
  89.         if (call == NULL) {
  90.             PyErr_Clear();
  91.             return 0;
  92.         }
  93.         /* Could test recursively but don't, for fear of endless
  94.            recursion if some joker sets self.__call__ = self */
  95.         Py_DECREF(call);
  96.         return 1;
  97.     }
  98.     return 0;
  99. }
  100. #endif
  101.  
  102. PyObject *
  103. PyObject_Type(o)
  104.     PyObject *o;
  105. {
  106.     PyObject *v;
  107.  
  108.     if(! o) return Py_ReturnNullError();
  109.     v = (PyObject *)o->ob_type;
  110.     Py_INCREF(v);
  111.     return v;
  112. }
  113.  
  114. int
  115. PyObject_Length(o)
  116.   PyObject *o;
  117. {
  118.   PySequenceMethods *m;
  119.  
  120.   if(! o) return Py_ReturnNullError(),-1;
  121.  
  122.   if((m=o->ob_type->tp_as_sequence) && m->sq_length)
  123.     return m->sq_length(o);
  124.  
  125.   return PyMapping_Length(o);
  126. }
  127.  
  128. PyObject *
  129. PyObject_GetItem(o, key)
  130.   PyObject *o;
  131.   PyObject *key;
  132. {
  133.   PyMappingMethods *m;
  134.  
  135.   if(! o || ! key) return Py_ReturnNullError();
  136.  
  137.   if((m=o->ob_type->tp_as_mapping) && m->mp_subscript)
  138.     return m->mp_subscript(o,key);
  139.   
  140.   if(PyInt_Check(key))
  141.     return PySequence_GetItem(o,PyInt_AsLong(key));
  142.  
  143.   PyErr_SetString(PyExc_TypeError,"expected integer index");
  144.   return NULL;
  145. }
  146.  
  147. int
  148. PyObject_SetItem(o, key, value)
  149.   PyObject *o;
  150.   PyObject *key;
  151.   PyObject *value;
  152. {
  153.   PyMappingMethods *m;
  154.  
  155.   if(! o || ! key || ! value) return Py_ReturnNullError(),-1;
  156.   if((m=o->ob_type->tp_as_mapping) && m->mp_ass_subscript)
  157.     return m->mp_ass_subscript(o,key,value);
  158.   
  159.   if(PyInt_Check(key))
  160.     return PySequence_SetItem(o,PyInt_AsLong(key),value);
  161.  
  162.   PyErr_SetString(PyExc_TypeError,"expected integer index");
  163.   return -1;
  164. }
  165.  
  166. int
  167. PyObject_DelItem(o, key)
  168.   PyObject *o;
  169.   PyObject *key;
  170. {
  171.   PyMappingMethods *m;
  172.  
  173.   if(! o || ! key) return Py_ReturnNullError(),-1;
  174.   if((m=o->ob_type->tp_as_mapping) && m->mp_ass_subscript)
  175.     return m->mp_ass_subscript(o,key,(PyObject*)NULL);
  176.   
  177.   if(PyInt_Check(key))
  178.     return PySequence_SetItem(o,PyInt_AsLong(key),(PyObject*)NULL);
  179.  
  180.   PyErr_SetString(PyExc_TypeError,"expected integer index");
  181.   return -1;
  182. }
  183.  
  184. int 
  185. PyNumber_Check(o)
  186.   PyObject *o;
  187. {
  188.   return o && o->ob_type->tp_as_number;
  189. }
  190.  
  191.  
  192. #define BINOP(opname, ropname, thisfunc) \
  193.     if (!PyInstance_Check(v) && !PyInstance_Check(w)) \
  194.         ; \
  195.     else \
  196.         return PyInstance_DoBinOp(v, w, opname, ropname, thisfunc)
  197.  
  198. PyObject *
  199. PyNumber_Or(v, w)
  200.     PyObject *v, *w;
  201. {
  202.         extern int PyNumber_Coerce();
  203.  
  204.     BINOP("__or__", "__ror__", PyNumber_Or);
  205.     if (v->ob_type->tp_as_number != NULL) {
  206.         PyObject *x = NULL;
  207.         PyObject * (*f) Py_FPROTO((PyObject *, PyObject *));
  208.         if (PyNumber_Coerce(&v, &w) != 0)
  209.             return NULL;
  210.         if ((f = v->ob_type->tp_as_number->nb_or) != NULL)
  211.             x = (*f)(v, w);
  212.         Py_DECREF(v);
  213.         Py_DECREF(w);
  214.         if (f != NULL)
  215.             return x;
  216.     }
  217.     PyErr_SetString(PyExc_TypeError, "bad operand type(s) for |");
  218.     return NULL;
  219. }
  220.  
  221. PyObject *
  222. PyNumber_Xor(v, w)
  223.     PyObject *v, *w;
  224. {
  225.         extern int PyNumber_Coerce();
  226.  
  227.     BINOP("__xor__", "__rxor__", PyNumber_Xor);
  228.     if (v->ob_type->tp_as_number != NULL) {
  229.         PyObject *x = NULL;
  230.         PyObject * (*f) Py_FPROTO((PyObject *, PyObject *));
  231.         if (PyNumber_Coerce(&v, &w) != 0)
  232.             return NULL;
  233.         if ((f = v->ob_type->tp_as_number->nb_xor) != NULL)
  234.             x = (*f)(v, w);
  235.         Py_DECREF(v);
  236.         Py_DECREF(w);
  237.         if (f != NULL)
  238.             return x;
  239.     }
  240.     PyErr_SetString(PyExc_TypeError, "bad operand type(s) for ^");
  241.     return NULL;
  242. }
  243.  
  244. PyObject *
  245. PyNumber_And(v, w)
  246.     PyObject *v, *w;
  247. {
  248.     BINOP("__and__", "__rand__", PyNumber_And);
  249.     if (v->ob_type->tp_as_number != NULL) {
  250.         PyObject *x = NULL;
  251.         PyObject * (*f) Py_FPROTO((PyObject *, PyObject *));
  252.         if (PyNumber_Coerce(&v, &w) != 0)
  253.             return NULL;
  254.         if ((f = v->ob_type->tp_as_number->nb_and) != NULL)
  255.             x = (*f)(v, w);
  256.         Py_DECREF(v);
  257.         Py_DECREF(w);
  258.         if (f != NULL)
  259.             return x;
  260.     }
  261.     PyErr_SetString(PyExc_TypeError, "bad operand type(s) for &");
  262.     return NULL;
  263. }
  264.  
  265. PyObject *
  266. PyNumber_Lshift(v, w)
  267.     PyObject *v, *w;
  268. {
  269.     BINOP("__lshift__", "__rlshift__", PyNumber_Lshift);
  270.     if (v->ob_type->tp_as_number != NULL) {
  271.         PyObject *x = NULL;
  272.         PyObject * (*f) Py_FPROTO((PyObject *, PyObject *));
  273.         if (PyNumber_Coerce(&v, &w) != 0)
  274.             return NULL;
  275.         if ((f = v->ob_type->tp_as_number->nb_lshift) != NULL)
  276.             x = (*f)(v, w);
  277.         Py_DECREF(v);
  278.         Py_DECREF(w);
  279.         if (f != NULL)
  280.             return x;
  281.     }
  282.     PyErr_SetString(PyExc_TypeError, "bad operand type(s) for <<");
  283.     return NULL;
  284. }
  285.  
  286. PyObject *
  287. PyNumber_Rshift(v, w)
  288.     PyObject *v, *w;
  289. {
  290.     BINOP("__rshift__", "__rrshift__", PyNumber_Rshift);
  291.     if (v->ob_type->tp_as_number != NULL) {
  292.         PyObject *x = NULL;
  293.         PyObject * (*f) Py_FPROTO((PyObject *, PyObject *));
  294.         if (PyNumber_Coerce(&v, &w) != 0)
  295.             return NULL;
  296.         if ((f = v->ob_type->tp_as_number->nb_rshift) != NULL)
  297.             x = (*f)(v, w);
  298.         Py_DECREF(v);
  299.         Py_DECREF(w);
  300.         if (f != NULL)
  301.             return x;
  302.     }
  303.     PyErr_SetString(PyExc_TypeError, "bad operand type(s) for >>");
  304.     return NULL;
  305. }
  306.  
  307. PyObject *
  308. PyNumber_Add(v, w)
  309.     PyObject *v, *w;
  310. {
  311.     BINOP("__add__", "__radd__", PyNumber_Add);
  312.     if (v->ob_type->tp_as_sequence != NULL)
  313.         return (*v->ob_type->tp_as_sequence->sq_concat)(v, w);
  314.     else if (v->ob_type->tp_as_number != NULL) {
  315.         PyObject *x;
  316.         if (PyNumber_Coerce(&v, &w) != 0)
  317.             return NULL;
  318.         x = (*v->ob_type->tp_as_number->nb_add)(v, w);
  319.         Py_DECREF(v);
  320.         Py_DECREF(w);
  321.         return x;
  322.     }
  323.     PyErr_SetString(PyExc_TypeError, "bad operand type(s) for +");
  324.     return NULL;
  325. }
  326.  
  327. PyObject *
  328. PyNumber_Subtract(v, w)
  329.     PyObject *v, *w;
  330. {
  331.     BINOP("__sub__", "__rsub__", PyNumber_Subtract);
  332.     if (v->ob_type->tp_as_number != NULL) {
  333.         PyObject *x;
  334.         if (PyNumber_Coerce(&v, &w) != 0)
  335.             return NULL;
  336.         x = (*v->ob_type->tp_as_number->nb_subtract)(v, w);
  337.         Py_DECREF(v);
  338.         Py_DECREF(w);
  339.         return x;
  340.     }
  341.     PyErr_SetString(PyExc_TypeError, "bad operand type(s) for -");
  342.     return NULL;
  343. }
  344.  
  345. PyObject *
  346. PyNumber_Multiply(v, w)
  347.     PyObject *v, *w;
  348. {
  349.     PyTypeObject *tp;
  350.     tp = v->ob_type;
  351.     BINOP("__mul__", "__rmul__", PyNumber_Multiply);
  352.     if (tp->tp_as_number != NULL &&
  353.         w->ob_type->tp_as_sequence != NULL &&
  354.         !PyInstance_Check(v)) {
  355.         /* number*sequence -- swap v and w */
  356.         PyObject *tmp = v;
  357.         v = w;
  358.         w = tmp;
  359.         tp = v->ob_type;
  360.     }
  361.     if (tp->tp_as_number != NULL) {
  362.         PyObject *x;
  363.         if (PyInstance_Check(v)) {
  364.             /* Instances of user-defined classes get their
  365.                other argument uncoerced, so they may
  366.                implement sequence*number as well as
  367.                number*number. */
  368.             Py_INCREF(v);
  369.             Py_INCREF(w);
  370.         }
  371.         else if (PyNumber_Coerce(&v, &w) != 0)
  372.             return NULL;
  373.         x = (*v->ob_type->tp_as_number->nb_multiply)(v, w);
  374.         Py_DECREF(v);
  375.         Py_DECREF(w);
  376.         return x;
  377.     }
  378.     if (tp->tp_as_sequence != NULL) {
  379.         if (!PyInt_Check(w)) {
  380.             PyErr_SetString(PyExc_TypeError,
  381.                 "can't multiply sequence with non-int");
  382.             return NULL;
  383.         }
  384.         return (*tp->tp_as_sequence->sq_repeat)
  385.                         (v, (int)PyInt_AsLong(w));
  386.     }
  387.     PyErr_SetString(PyExc_TypeError, "bad operand type(s) for *");
  388.     return NULL;
  389. }
  390.  
  391. PyObject *
  392. PyNumber_Divide(v, w)
  393.     PyObject *v, *w;
  394. {
  395.     BINOP("__div__", "__rdiv__", PyNumber_Divide);
  396.     if (v->ob_type->tp_as_number != NULL) {
  397.         PyObject *x;
  398.         if (PyNumber_Coerce(&v, &w) != 0)
  399.             return NULL;
  400.         x = (*v->ob_type->tp_as_number->nb_divide)(v, w);
  401.         Py_DECREF(v);
  402.         Py_DECREF(w);
  403.         return x;
  404.     }
  405.     PyErr_SetString(PyExc_TypeError, "bad operand type(s) for /");
  406.     return NULL;
  407. }
  408.  
  409. PyObject *
  410. PyNumber_Remainder(v, w)
  411.     PyObject *v, *w;
  412. {
  413.     if (PyString_Check(v)) {
  414.         return PyString_Format(v, w);
  415.     }
  416.     BINOP("__mod__", "__rmod__", PyNumber_Remainder);
  417.     if (v->ob_type->tp_as_number != NULL) {
  418.         PyObject *x;
  419.         if (PyNumber_Coerce(&v, &w) != 0)
  420.             return NULL;
  421.         x = (*v->ob_type->tp_as_number->nb_remainder)(v, w);
  422.         Py_DECREF(v);
  423.         Py_DECREF(w);
  424.         return x;
  425.     }
  426.     PyErr_SetString(PyExc_TypeError, "bad operand type(s) for %");
  427.     return NULL;
  428. }
  429.  
  430. PyObject *
  431. PyNumber_Divmod(v, w)
  432.     PyObject *v, *w;
  433. {
  434.     PyObject *res;
  435.  
  436.     if (PyInstance_Check(v) || PyInstance_Check(w))
  437.         return PyInstance_DoBinOp(v, w, "__divmod__", "__rdivmod__",
  438.                      PyNumber_Divmod);
  439.     if (v->ob_type->tp_as_number == NULL ||
  440.                 w->ob_type->tp_as_number == NULL) {
  441.         PyErr_SetString(PyExc_TypeError,
  442.             "divmod() requires numeric or class instance arguments");
  443.         return NULL;
  444.     }
  445.     if (PyNumber_Coerce(&v, &w) != 0)
  446.         return NULL;
  447.     res = (*v->ob_type->tp_as_number->nb_divmod)(v, w);
  448.     Py_DECREF(v);
  449.     Py_DECREF(w);
  450.     return res;
  451. }
  452.  
  453.  
  454. static PyObject *
  455. do_pow(v, w)
  456.     PyObject *v, *w;
  457. {
  458.     PyObject *res;
  459.     if (PyInstance_Check(v) || PyInstance_Check(w))
  460.         return PyInstance_DoBinOp(v, w, "__pow__", "__rpow__", do_pow);
  461.     if (v->ob_type->tp_as_number == NULL ||
  462.         w->ob_type->tp_as_number == NULL) {
  463.         PyErr_SetString(PyExc_TypeError,
  464.                 "pow() requires numeric arguments");
  465.         return NULL;
  466.     }
  467.     if (PyFloat_Check(v) && PyFloat_Check(w) &&
  468.         PyFloat_AsDouble(v) < 0.0) {
  469.         if (!PyErr_Occurred())
  470.             PyErr_SetString(PyExc_ValueError,
  471.                     "negative number to float power");
  472.         return NULL;
  473.     }
  474.     if (PyNumber_Coerce(&v, &w) != 0)
  475.         return NULL;
  476.     res = (*v->ob_type->tp_as_number->nb_power)(v, w, Py_None);
  477.     Py_DECREF(v);
  478.     Py_DECREF(w);
  479.     return res;
  480. }
  481.  
  482. PyObject *
  483. PyNumber_Power(v,w,z)
  484.     PyObject *v, *w, *z;
  485. {
  486.     PyObject *res;
  487.     PyObject *v1, *z1, *w2, *z2;
  488.  
  489.     if (z == Py_None)
  490.         return do_pow(v, w);
  491.     /* XXX The ternary version doesn't do class instance coercions */
  492.     if (PyInstance_Check(v))
  493.         return v->ob_type->tp_as_number->nb_power(v, w, z);
  494.     if (v->ob_type->tp_as_number == NULL ||
  495.         z->ob_type->tp_as_number == NULL ||
  496.         w->ob_type->tp_as_number == NULL) {
  497.         PyErr_SetString(PyExc_TypeError, "pow() requires numeric arguments");
  498.         return NULL;
  499.     }
  500.     if (PyNumber_Coerce(&v, &w) != 0)
  501.         return NULL;
  502.     res = NULL;
  503.     v1 = v;
  504.     z1 = z;
  505.     if (PyNumber_Coerce(&v1, &z1) != 0)
  506.         goto error2;
  507.     w2 = w;
  508.     z2 = z1;
  509.      if (PyNumber_Coerce(&w2, &z2) != 0)
  510.         goto error1;
  511.     res = (*v1->ob_type->tp_as_number->nb_power)(v1, w2, z2);
  512.     Py_DECREF(w2);
  513.     Py_DECREF(z2);
  514.  error1:
  515.     Py_DECREF(v1);
  516.     Py_DECREF(z1);
  517.  error2:
  518.     Py_DECREF(v);
  519.     Py_DECREF(w);
  520.     return res;
  521. }
  522.  
  523.  
  524. PyObject *
  525. PyNumber_Negative(v)
  526.     PyObject *v;
  527. {
  528.     if (v->ob_type->tp_as_number != NULL)
  529.         return (*v->ob_type->tp_as_number->nb_negative)(v);
  530.     PyErr_SetString(PyExc_TypeError, "bad operand type(s) for unary -");
  531.     return NULL;
  532. }
  533.  
  534. PyObject *
  535. PyNumber_Positive(v)
  536.     PyObject *v;
  537. {
  538.     if (v->ob_type->tp_as_number != NULL)
  539.         return (*v->ob_type->tp_as_number->nb_positive)(v);
  540.     PyErr_SetString(PyExc_TypeError, "bad operand type(s) for unary +");
  541.     return NULL;
  542. }
  543.  
  544. PyObject *
  545. PyNumber_Invert(v)
  546.     PyObject *v;
  547. {
  548.     PyObject * (*f) Py_FPROTO((PyObject *));
  549.     if (v->ob_type->tp_as_number != NULL &&
  550.         (f = v->ob_type->tp_as_number->nb_invert) != NULL)
  551.         return (*f)(v);
  552.     PyErr_SetString(PyExc_TypeError, "bad operand type(s) for unary ~");
  553.     return NULL;
  554. }
  555.  
  556. PyObject *
  557. PyNumber_Absolute(o)
  558.   PyObject *o;
  559. {
  560.   PyNumberMethods *m;
  561.  
  562.   if(! o) return Py_ReturnNullError();
  563.   if((m=o->ob_type->tp_as_number) && m->nb_absolute)
  564.     return m->nb_absolute(o);
  565.  
  566.   return Py_ReturnMethodError("__abs__");  
  567. }
  568.  
  569. PyObject *
  570. PyNumber_Int(o)
  571.   PyObject *o;
  572. {
  573.   PyNumberMethods *m;
  574.  
  575.   if(! o) return Py_ReturnNullError();
  576.   if((m=o->ob_type->tp_as_number) && m->nb_int)
  577.     return m->nb_int(o);
  578.  
  579.   return Py_ReturnMethodError("__int__");  
  580. }
  581.  
  582. PyObject *
  583. PyNumber_Long(o)
  584.   PyObject *o;
  585. {
  586.   PyNumberMethods *m;
  587.  
  588.   if(! o) return Py_ReturnNullError();
  589.   if((m=o->ob_type->tp_as_number) && m->nb_long)
  590.     return m->nb_long(o);
  591.  
  592.   return Py_ReturnMethodError("__long__");  
  593. }
  594.  
  595. PyObject *
  596. PyNumber_Float(o)
  597.   PyObject *o;
  598. {
  599.   PyNumberMethods *m;
  600.  
  601.   if(! o) return Py_ReturnNullError();
  602.   if((m=o->ob_type->tp_as_number) && m->nb_float)
  603.     return m->nb_float(o);
  604.  
  605.   return Py_ReturnMethodError("__float__");  
  606. }
  607.  
  608.  
  609. int 
  610. PySequence_Check(o)
  611.   PyObject *o;
  612. {
  613.   return o && o->ob_type->tp_as_sequence;
  614. }
  615.  
  616. int 
  617. PySequence_Length(s)
  618.   PyObject *s;
  619. {
  620.   PySequenceMethods *m;
  621.  
  622.   if(! s) return Py_ReturnNullError(),-1;
  623.  
  624.   if((m=s->ob_type->tp_as_sequence) && m->sq_length)
  625.     return m->sq_length(s);
  626.  
  627.   Py_ReturnMethodError("__len__");
  628.   return -1;
  629. }
  630.  
  631. PyObject *
  632. PySequence_Concat(s, o)
  633.   PyObject *s;
  634.   PyObject *o;
  635. {
  636.   PySequenceMethods *m;
  637.  
  638.   if(! s || ! o) return Py_ReturnNullError();
  639.       
  640.   if((m=s->ob_type->tp_as_sequence) && m->sq_concat)
  641.     return m->sq_concat(s,o);
  642.  
  643.   return Py_ReturnMethodError("__concat__");
  644. }
  645.  
  646. PyObject *
  647. PySequence_Repeat(o, count)
  648.   PyObject *o;
  649.   int count;
  650. {
  651.   PySequenceMethods *m;
  652.  
  653.   if(! o) return Py_ReturnNullError();
  654.       
  655.   if((m=o->ob_type->tp_as_sequence) && m->sq_repeat)
  656.     return m->sq_repeat(o,count);
  657.  
  658.   return Py_ReturnMethodError("__repeat__");
  659. }
  660.  
  661. PyObject *
  662. PySequence_GetItem(s, i)
  663.   PyObject *s;
  664.   int i;
  665. {
  666.   PySequenceMethods *m;
  667.   int l;
  668.  
  669.   if(! s) return Py_ReturnNullError();
  670.  
  671.   if(! ((m=s->ob_type->tp_as_sequence) && m->sq_item))
  672.     return Py_ReturnMethodError("__getitem__");  
  673.  
  674.   if(i < 0)
  675.     {
  676.       if(! m->sq_length || 0 > (l=m->sq_length(s))) return NULL;
  677.       i += l;
  678.     }
  679.       
  680.   return m->sq_item(s,i);
  681. }
  682.  
  683. PyObject *
  684. PySequence_GetSlice(s, i1, i2)
  685.   PyObject *s;
  686.   int i1;
  687.   int i2;
  688. {
  689.   PySequenceMethods *m;
  690.   int l;
  691.  
  692.   if(! s) return Py_ReturnNullError();
  693.  
  694.   if(! ((m=s->ob_type->tp_as_sequence) && m->sq_slice))
  695.     return Py_ReturnMethodError("__getslice__");  
  696.  
  697.   if(i1 < 0 || i2 < 0)
  698.     {
  699.  
  700.       if(! m->sq_length || 0 > (l=m->sq_length(s))) return NULL;
  701.  
  702.       if(i1 < 0) i1 += l;
  703.       if(i2 < 0) i2 += l;
  704.     }
  705.       
  706.   return m->sq_slice(s,i1,i2);
  707. }
  708.  
  709. int
  710. PySequence_SetItem(s, i, o)
  711.   PyObject *s;
  712.   int i;
  713.   PyObject *o;
  714. {
  715.   PySequenceMethods *m;
  716.   int l;
  717.   if(! s) return Py_ReturnNullError(),-1;
  718.  
  719.   if(! ((m=s->ob_type->tp_as_sequence) && m->sq_length && m->sq_ass_item))
  720.     return Py_ReturnMethodError("__setitem__"),-1;  
  721.  
  722.   if(i < 0)
  723.     {
  724.       if(0 > (l=m->sq_length(s))) return -1;
  725.       i += l;
  726.     }
  727.       
  728.   return m->sq_ass_item(s,i,o);
  729. }
  730.  
  731. int
  732. PySequence_DelItem(s, i)
  733.   PyObject *s;
  734.   int i;
  735. {
  736.   PySequenceMethods *m;
  737.   int l;
  738.   if(! s) return Py_ReturnNullError(),-1;
  739.  
  740.   if(! ((m=s->ob_type->tp_as_sequence) && m->sq_length && m->sq_ass_item))
  741.     return Py_ReturnMethodError("__delitem__"),-1;  
  742.  
  743.   if(i < 0)
  744.     {
  745.       if(0 > (l=m->sq_length(s))) return -1;
  746.       i += l;
  747.     }
  748.       
  749.   return m->sq_ass_item(s,i,(PyObject*)NULL);
  750. }
  751.  
  752. int 
  753. PySequence_SetSlice(s, i1, i2, o)
  754.   PyObject *s;
  755.   int i1;
  756.   int i2;
  757.   PyObject *o;
  758. {
  759.   PySequenceMethods *m;
  760.   int l;
  761.  
  762.   if(! s) return Py_ReturnNullError(),-1;
  763.  
  764.   if(! ((m=s->ob_type->tp_as_sequence) && m->sq_length && m->sq_ass_slice))
  765.     return Py_ReturnMethodError("__setslice__"),-1;  
  766.  
  767.   if(0 > (l=m->sq_length(s))) return -1;
  768.  
  769.   if(i1 < 0) i1 += l;
  770.   if(i2 < 0) i2 += l;
  771.       
  772.   return m->sq_ass_slice(s,i1,i2,o);
  773. }
  774.  
  775. int 
  776. PySequence_DelSlice(s, i1, i2)
  777.   PyObject *s;
  778.   int i1;
  779.   int i2;
  780. {
  781.   PySequenceMethods *m;
  782.   int l;
  783.  
  784.   if(! s) return Py_ReturnNullError(),-1;
  785.  
  786.   if(! ((m=s->ob_type->tp_as_sequence) && m->sq_length && m->sq_ass_slice))
  787.     return Py_ReturnMethodError("__delslice__"),-1;  
  788.  
  789.   if(0 > (l=m->sq_length(s))) return -1;
  790.  
  791.   if(i1 < 0) i1 += l;
  792.   if(i2 < 0) i2 += l;
  793.       
  794.   return m->sq_ass_slice(s,i1,i2,(PyObject*)NULL);
  795. }
  796.  
  797. PyObject *
  798. PySequence_Tuple(s)
  799.   PyObject *s;
  800. {
  801.   int l, i;
  802.   PyObject *t, *item;
  803.  
  804.   if(! s) return Py_ReturnNullError();
  805.  
  806.   Py_TRY((l=PySequence_Length(s)) != -1);
  807.   Py_TRY(t=PyTuple_New(l));
  808.  
  809.   for(i=0; i < l; i++)
  810.     {
  811.       if(!(item=PySequence_GetItem(s,i)) ||
  812.      PyTuple_SetItem(t,i,item) == -1)
  813.     {
  814.       Py_DECREF(t);
  815.       return NULL;
  816.     }
  817.     }
  818.   return t;
  819. }
  820.  
  821. PyObject *
  822. PySequence_List(s)
  823.   PyObject *s;
  824. {
  825.   int l, i;
  826.   PyObject *t, *item;
  827.  
  828.   if(! s) return Py_ReturnNullError();
  829.  
  830.   Py_TRY((l=PySequence_Length(s)) != -1);
  831.   Py_TRY(t=PyList_New(l));
  832.  
  833.   for(i=0; i < l; i++)
  834.     {
  835.       if(!(item=PySequence_GetItem(s,i)) ||
  836.      PyList_SetItem(t,i,item) == -1)
  837.     {
  838.       Py_DECREF(t);
  839.       return NULL;
  840.     }
  841.     }
  842.   return t;
  843. }
  844.  
  845. int 
  846. PySequence_Count(s, o)
  847.   PyObject *s;
  848.   PyObject *o;
  849. {
  850.   int l, i, n=0, not_equal, err;
  851.   PyObject *item;
  852.  
  853.   if(! s || ! o) return Py_ReturnNullError(), -1;
  854.   if((l=PySequence_Length(s)) == -1) return -1;
  855.  
  856.   for(i=0; i < l; i++)
  857.     {
  858.       if((item=PySequence_GetItem(s,i)) == NULL) return -1;
  859.       err=PyObject_Cmp(item,o,¬_equal) == -1;
  860.       Py_DECREF(item);
  861.       if(err) return -1;
  862.       n += ! not_equal;
  863.     }
  864.   return n;
  865. }
  866.  
  867. int 
  868. PySequence_In(s, o)
  869.   PyObject *s;
  870.   PyObject *o;
  871. {
  872.   int l, i, not_equal, err;
  873.   PyObject *item;
  874.  
  875.   if(! o || ! s) return Py_ReturnNullError(), -1;
  876.   if((l=PySequence_Length(s)) == -1) return -1;
  877.  
  878.   for(i=0; i < l; i++)
  879.     {
  880.       if((item=PySequence_GetItem(s,i)) == NULL) return -1;
  881.       err=PyObject_Cmp(item,o,¬_equal) == -1;
  882.       Py_DECREF(item);
  883.       if(err) return -1;
  884.       if(! not_equal) return 1;
  885.     }
  886.   return 0;
  887. }
  888.  
  889. int 
  890. PySequence_Index(s, o)
  891.   PyObject *s;
  892.   PyObject *o;
  893. {
  894.   int l, i, not_equal, err;
  895.   PyObject *item;
  896.  
  897.   if(! s || ! o) return Py_ReturnNullError(), -1;
  898.   if((l=PySequence_Length(s)) == -1) return -1;
  899.  
  900.   for(i=0; i < l; i++)
  901.     {
  902.       if((item=PySequence_GetItem(s,i)) == NULL) return -1;
  903.       err=PyObject_Cmp(item,o,¬_equal) == -1;
  904.       Py_DECREF(item);
  905.       if(err) return -1;
  906.       if(! not_equal) return i;
  907.     }
  908.   PyErr_SetString(PyExc_ValueError, "list.index(x): x not in list");
  909.   return -1;
  910. }
  911.  
  912. int 
  913. PyMapping_Check(o)
  914.   PyObject *o;
  915. {
  916.   return o && o->ob_type->tp_as_mapping;
  917. }
  918.  
  919. int 
  920. PyMapping_Length(s)
  921.   PyObject *s;
  922. {
  923.   PyMappingMethods *m;
  924.  
  925.   if(! s) return Py_ReturnNullError(),-1;
  926.  
  927.   if((m=s->ob_type->tp_as_mapping) && m->mp_length)
  928.     return m->mp_length(s);
  929.  
  930.   Py_ReturnMethodError("__len__");
  931.   return -1;
  932. }
  933.  
  934. int 
  935. PyMapping_HasKeyString(o, key)
  936.   PyObject *o;
  937.   char *key;
  938. {
  939.   PyObject *v;
  940.  
  941.   v=PyMapping_GetItemString(o,key);
  942.   if(v) {
  943.     Py_DECREF(v);
  944.     return 1;
  945.   }
  946.   PyErr_Clear();
  947.   return 0;
  948. }
  949.  
  950. int 
  951. PyMapping_HasKey(o, key)
  952.   PyObject *o;
  953.   PyObject *key;
  954. {
  955.   PyObject *v;
  956.  
  957.   v=PyObject_GetItem(o,key);
  958.   if(v) {
  959.     Py_DECREF(v);
  960.     return 1;
  961.   }
  962.   PyErr_Clear();
  963.   return 0;
  964. }
  965.  
  966. PyObject *
  967. PyObject_CallObject(o, a)
  968.   PyObject *o, *a;
  969. {
  970.   PyObject *r;
  971.  
  972.   if(a) return PyEval_CallObject(o,a);
  973.  
  974.   if(! (a=PyTuple_New(0)))
  975.     return NULL;
  976.   r=PyEval_CallObject(o,a);
  977.   Py_DECREF(a);
  978.   return r;
  979.  
  980. PyObject *
  981. #ifdef HAVE_STDARG_PROTOTYPES
  982. /* VARARGS 2 */
  983. PyObject_CallFunction(PyObject *PyCallable_Check, char *format, ...)
  984. #else
  985. /* VARARGS */
  986. PyObject_CallFunction(va_alist) va_dcl
  987. #endif
  988. {
  989.   va_list va;
  990.   PyObject *args, *retval;
  991. #ifdef HAVE_STDARG_PROTOTYPES
  992.   va_start(va, format);
  993. #else
  994.   PyObject *PyCallable_Check;
  995.   char *format;
  996.   va_start(va);
  997.   PyCallable_Check = va_arg(va, PyObject *);
  998.   format   = va_arg(va, char *);
  999. #endif
  1000.  
  1001.   if( ! PyCallable_Check)
  1002.     {
  1003.       va_end(va);
  1004.       return Py_ReturnNullError();
  1005.     }
  1006.  
  1007.   if(format)
  1008.     args = Py_VaBuildValue(format, va);
  1009.   else
  1010.     args = PyTuple_New(0);
  1011.   
  1012.   va_end(va);
  1013.   if(! args) return NULL;
  1014.  
  1015.   if(! PyTuple_Check(args))
  1016.     {
  1017.       PyObject *a;
  1018.       
  1019.       Py_TRY(a=PyTuple_New(1));
  1020.       Py_TRY(PyTuple_SetItem(a,0,args) != -1);
  1021.       args=a;
  1022.     }
  1023.   retval = PyObject_CallObject(PyCallable_Check,args);
  1024.   Py_DECREF(args);
  1025.   return retval;
  1026. }
  1027.  
  1028. PyObject *
  1029. #ifdef HAVE_STDARG_PROTOTYPES
  1030. /* VARARGS 2 */
  1031. PyObject_CallMethod(PyObject *o, char *name, char *format, ...)
  1032. #else
  1033. /* VARARGS */
  1034. PyObject_CallMethod(va_alist) va_dcl
  1035. #endif
  1036. {
  1037.   va_list va;
  1038.   PyObject *args, *func=0, *retval;
  1039. #ifdef HAVE_STDARG_PROTOTYPES
  1040.   va_start(va, format);
  1041. #else
  1042.   PyObject *o;
  1043.   char *name;
  1044.   char *format;
  1045.   va_start(va);
  1046.   o      = va_arg(va, PyObject *);
  1047.   name   = va_arg(va, char *);
  1048.   format = va_arg(va, char *);
  1049. #endif
  1050.  
  1051.   if( ! o || ! name)
  1052.     {
  1053.       va_end(va);
  1054.       return Py_ReturnNullError();
  1055.     }
  1056.  
  1057.   func=PyObject_GetAttrString(o,name);
  1058.   if(! func)
  1059.     {
  1060.       va_end(va);
  1061.       PyErr_SetString(PyExc_AttributeError,name);
  1062.       return 0;
  1063.     }
  1064.    
  1065.   if(! (PyCallable_Check(func)))
  1066.     {
  1067.       va_end(va);
  1068.       PyErr_SetString(PyExc_TypeError,"call of non-callable attribute");
  1069.       return 0;
  1070.     }
  1071.  
  1072.   if(format && *format)
  1073.     args = Py_VaBuildValue(format, va);
  1074.   else
  1075.     args = PyTuple_New(0);
  1076.   
  1077.   va_end(va);
  1078.  
  1079.   if(! args) return NULL;
  1080.  
  1081.   if(! PyTuple_Check(args))
  1082.     {
  1083.       PyObject *a;
  1084.       
  1085.       Py_TRY(a=PyTuple_New(1));
  1086.       Py_TRY(PyTuple_SetItem(a,0,args) != -1);
  1087.       args=a;
  1088.     }
  1089.  
  1090.   retval = PyObject_CallObject(func,args);
  1091.   Py_DECREF(args);
  1092.   Py_DECREF(func);
  1093.   return retval;
  1094. }
  1095.  
  1096. PyObject *
  1097. PyMapping_GetItemString(o, key)
  1098.   PyObject *o;
  1099.   char *key;
  1100. {
  1101.   PyObject *okey, *r;
  1102.  
  1103.   if( ! key) return Py_ReturnNullError();
  1104.   Py_TRY(okey=PyString_FromString(key));
  1105.   r = PyObject_GetItem(o,okey);
  1106.   Py_DECREF(okey);
  1107.   return r;
  1108. }
  1109.  
  1110. int
  1111. PyMapping_SetItemString(o, key, value)
  1112.  PyObject *o;
  1113.  char *key;
  1114.  PyObject *value;
  1115. {
  1116.   PyObject *okey;
  1117.   int r;
  1118.  
  1119.   if( ! key) return Py_ReturnNullError(),-1;
  1120.   if (!(okey=PyString_FromString(key))) return -1;
  1121.   r = PyObject_SetItem(o,okey,value);
  1122.   Py_DECREF(okey);
  1123.   return r;
  1124. }
  1125.